import random

class LiuYao:
    def bu(self):
        # 卦象是长度为6的列表，存储六个爻
        gua = [0] * 6
        # 三易为一爻，一共六爻，所以循环六次
        for j in range(6, 0, -1):
            # 大衍之数五十，用其四十有九
            all_num = 49
            # 统计最后三易的结果
            result = 0
            # 三易，循环三次
            for i in range(3):
                # 分二，使用随机数
                left = random.randint(1, all_num - 2)
                # 右手代表地，需要挂一
                right = all_num - left - 1
                # 揲四，如果刚好剩四个求余拿不到，就直接减
                if left % 4 == 0:
                    all_num -= 4
                if right % 4 == 0:
                    all_num -= 4
                # 归奇，最后要减掉挂一的那一根
                all_num -= left % 4 + right % 4 + 1
            # 统计最后剩几束，代表一爻
            result = all_num // 4
            # 将爻存入列表
            gua[j - 1] = result
        return gua

    def bian(self, ben):
        bian = [0] * 6
        # 爻变，6->7老阴变少阳，9->8老阳变少阴
        for i in range(6):
            if ben[i] == 6:
                bian[i] = 7
            elif ben[i] == 9:
                bian[i] = 8
            else:
                bian[i] = ben[i]
        return bian

    def xiang(self, gua):
        # 显示卦象
        for k in gua:
            if k == 6:
                print("■■■ ■■■x")
            elif k == 7:
                print("■■■■■■■")
            elif k == 8:
                print("■■■ ■■■")
            elif k == 9:
                print("■■■■■■■o")

    def jie(self, gua):
        sum_num = sum(gua)
        print("主变爻在：" + str(55 - sum_num))

if __name__ == "__main__":
    liuyao = LiuYao()
    gua = liuyao.bu()
    bian = liuyao.bian(gua)
    print("本卦：")
    liuyao.xiang(gua)
    print("变卦：")
    liuyao.xiang(bian)
    liuyao.jie(gua)
